package com.lin.dynamicProxyMode;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 客户
 */
public class Client {
    public static void main(String[] args) {
        UserService userService = ProxyUtils.creatProxy(new UserServiceImpl());

        UserService realService = new UserServiceImpl();
        UserService userService2 = (UserService) Proxy.newProxyInstance(
                UserService.class.getClassLoader(),
                new Class[] {UserService.class},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        log(method.getName());
                        return method.invoke(realService, args);
                    }
                    private static void log(String msg) {
                        System.out.println("使用了" + msg + "方法");
                    }
                }
        );

        userService.add();
        userService.delete();
        userService.update();
        userService.query();

        userService2.add();
        userService2.delete();
        userService2.update();
        userService2.query();

        // 代理角色  --代理类 --静态代理方式
        /*UserProxy userProxy = new UserProxy();
        userProxy.setUserService(userService);

        userProxy.add();
        userProxy.delete();
        userProxy.update();
        userProxy.query();*/
    }
}
